package com.tubang.logistics.controller.finance;

import com.tubang.common.util.ResultData;
import com.tubang.logistics.exception.BizException;
import com.tubang.logistics.service.IFinanceService;
import com.tubang.logistics.util.DateUtils;
import com.tubang.logistics.vo.finance.BillDetailVO;
import com.tubang.logistics.vo.finance.BillUpdateVO;
import com.tubang.logistics.vo.finance.SettleDetailVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("finance")
public class FinanceController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IFinanceService iFinanceService;

	@RequestMapping(value = "/bill", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listBillStatistics(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean get,
			@RequestParam Integer page,
			@RequestParam Integer rows) {
		try {
			Map<String, Object> result = iFinanceService.listBillStatistics(startTime, endTime, userId, get, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).
					setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/bill/table", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listBillTable(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean get,
			@RequestParam Integer page,
			@RequestParam Integer rows,
			@RequestParam(required = false) String content) {
		try {
			Map<String, Object> result = iFinanceService.listBillTable(startTime, endTime, userId, get, content, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/bill/export", method = RequestMethod.GET)
	public @ResponseBody
	ResultData exportBill(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean get,
			@RequestParam(required = false) String content,
			HttpServletResponse response) {
		try {
			HSSFWorkbook workbook = iFinanceService.exportBill(startTime, endTime, userId, get, content);

			//拼接文件名
			//String yearMonthDayNow = DateUtils.getYearMonthDayNow();
			//String filename = "账单详情_" + yearMonthDayNow + ".xls";
			String filename = "账单详情.xls";

			//设置头信息
			response.setContentType("Application/msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1"));

			//文件下载
			OutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			return ResultData.result(true).setMsg("导出成功");
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("导出异常");
		}
	}

	@RequestMapping(value = "/settle/export", method = RequestMethod.GET)
	public @ResponseBody
	ResultData exportSettle(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean get,
			@RequestParam boolean receive,
			@RequestParam(required = false) String content,
			HttpServletResponse response) {
		try {
			HSSFWorkbook workbook = iFinanceService.exportSettle(startTime, endTime, userId, get, receive, content);

			//拼接文件名
			//String yearMonthDayNow = DateUtils.getYearMonthDayNow();
			//String filename = "结算单详情_" + yearMonthDayNow + ".xls";
			String filename = "结算单详情.xls";

			//设置头信息
			response.setContentType("Application/msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1"));

			//文件下载
			OutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			return ResultData.result(true).setMsg("导出成功");
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("导出异常");
		}
	}

	@RequestMapping(value = "/bill/list", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listBill(
			@RequestParam(required = false) Long startTime,
			@RequestParam(required = false) Long endTime,
			@RequestParam String userId,
			@RequestParam Integer page,
			@RequestParam Integer rows,
			@RequestParam(required = false) Integer financeStatus,
			@RequestParam(required = false) String content) {
		try {
			Map<String, Object> result = iFinanceService.listBill(startTime, endTime, userId, financeStatus, content, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/bill", method = RequestMethod.PUT)
	public @ResponseBody
	ResultData updateBill(
			@RequestBody BillUpdateVO billUpdateVO) {
		try {
			iFinanceService.updateBill(billUpdateVO);
			return ResultData.result(true).setMsg("更新成功");
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("更新异常");
		}
	}

	@RequestMapping(value = "/bill/detail/{id}", method = RequestMethod.GET)
	public @ResponseBody
	ResultData getBillDetail(
			@PathVariable String id) {
		try {
			BillDetailVO billDetailVO = iFinanceService.getBillDetail(id);
			return ResultData.result(true).setMsg("查询成功").setData(billDetailVO);
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/settle", method = RequestMethod.POST)
	public @ResponseBody
	ResultData insertSettle(
			@RequestParam List<String> ids) {
		try {
			iFinanceService.insertSettle(ids);
			return ResultData.result(true).setMsg("生成成功");
		} catch (BizException bizE) {
			return ResultData.result(false).setMsg(bizE.getMessage());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("生成异常");
		}
	}

	@RequestMapping(value = "/settle/{id}", method = RequestMethod.PUT)
	public @ResponseBody
	ResultData insertSettle(
			@PathVariable String id) {
		try {
			iFinanceService.updateSettle(id);
			return ResultData.result(true).setMsg("更新成功");
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("更新异常");
		}
	}

	@RequestMapping(value = "/settle", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listSettle(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean get,
			@RequestParam boolean receive,
			@RequestParam Integer page,
			@RequestParam Integer rows) {
		try {
			Map<String, Object> result = iFinanceService.listSettle(startTime, endTime, userId, get, receive, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).
					setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/settle/list", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listSettleForCheck(
			@RequestParam(required = false) Long startTime,
			@RequestParam(required = false) Long endTime,
			@RequestParam String userId,
			@RequestParam(required = false) String content,
			@RequestParam(required = false) Integer status,
			@RequestParam boolean carrier,
			@RequestParam Integer page,
			@RequestParam Integer rows) {
		try {
			Map<String, Object> result = iFinanceService.listSettleForCheck(startTime, endTime, userId, content, status, carrier, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).
					setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/settle/detail/{id}", method = RequestMethod.GET)
	public @ResponseBody
	ResultData getSettleDetail(
			@PathVariable String id) {
		try {
			List<SettleDetailVO> result = iFinanceService.getSettleDetail(id);
			return ResultData.result(true).setMsg("查询成功").setData(result);
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}

	@RequestMapping(value = "/settle/table", method = RequestMethod.GET)
	public @ResponseBody
	ResultData listSettleTable(
			@RequestParam Long startTime,
			@RequestParam Long endTime,
			@RequestParam String userId,
			@RequestParam boolean get,
			@RequestParam boolean receive,
			@RequestParam Integer page,
			@RequestParam Integer rows,
			@RequestParam(required = false) String content) {
		try {
			Map<String, Object> result = iFinanceService.listSettleTable(startTime, endTime, userId, get, receive, content, page, rows);
			return ResultData.result(true).setMsg("查询成功").setData(result.get("result")).setTotalCount(((Long) result.get("total")).intValue());
		} catch (Exception e) {
			logger.error("", e);
			return ResultData.result(false).setMsg("查询异常");
		}
	}


}
